########################################################################
######### Converts original dyadic data into dyadic covariates #########
########################################################################

### Dyadic covariates: alliance, trade, contiguity

########################################################################
# Install packages
detach(package:statnet, unload=T)
install.packages("igraph")
install.packages("countrycode")
install.packages("plyr")
########################################################################

# Set the working directory
setwd("~/Dropbox/ISQ_ms/5 Final/Replication")

# load libraries
library(igraph)
library(countrycode)
library(plyr)

########################################################################
############ Converts alliance data into dyadic covariate  ##############
########################################################################

### Accomplishes 3 tasks:
#### 1) Inserts country codes for dyadic data
#### 2) Inserts COW-character country code
#### 3) For every year in DIPCON data, creates a dyadic covariate ("allyXXXX.csv")

########################################################################

# Read in original data
ally <- read.csv("atop3_0dy.csv",stringsAsFactors=F)

# check column names
names(ally)

# ATOPALLY gives an edgelist
table(ally$atopally)

# extract variables of interest
ally <- ally[,c("dyad", "year")]


#### 1) Insert country codes for dyadic data

# file only has dyad #; load file w/ country labels
labels <- read.csv("atop3_0ddyr.csv", stringsAsFactors=F)

# check column names
names(labels)

# check if 2nd file has larger # of dyads
length(unique(ally$dyad))
length(unique(labels$ddyad))

# extract variables of interest
labels <- labels[,c("ddyad", "stateA", "stateB")]

# get unique list of dyads from larger file
labels <- labels[!duplicated(labels$ddyad),]

# merge objects to get country labels
# make sure objects have the same colnames
colnames(labels) <- c("dyad", "stateA", "stateB")

# join
ally <- join(ally, labels, by="dyad", type="inner") 
colnames(ally) <- c("dyad", "year", "ccode1", "ccode2")


#### 2) Insert COW-character country code

# Convert to COW-character
ally$stateabb1 <- countrycode(ally$ccode1, "cown", "cowc")
ally$stateabb2 <- countrycode(ally$ccode2, "cown", "cowc")

# Check if all states have this code
table(is.na(ally$stateabb1))
table(is.na(ally$stateabb2))
ally[is.na(ally$stateabb2),] # ok, NAs only 1912-1915


#### 3) For every year in DIPCON data, create a dyadic covariate ("allyXXXX.csv")

# select COW DIP / DIPCON data years
table(ally$year)

ally1970 <- ally[ally$year=="1970",]
ally1975 <- ally[ally$year=="1975",]
ally1980 <- ally[ally$year=="1980",]
ally1985 <- ally[ally$year=="1985",]
ally1990 <- ally[ally$year=="1990",]
ally1995 <- ally[ally$year=="1995",]
ally2000 <- ally[ally$year=="2000",]
ally2003 <- ally[ally$year=="2003",]


### For 1970

# get IDs for all states in the dip data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1970,]

# create alliance edgelist
el1970 <- ally1970[,c("stateabb1", "stateabb2")]

# check number of states in el
uniq.el <- data.frame(unique(c(el1970$stateabb1, el1970$stateabb2)))
colnames(uniq.el) <- "stateabb"

# check if all states in el have stateIDs
test <- uniq.el$stateabb %in% stateIDs$stateabb
table(test)
uniq.el$stateabb[!uniq.el$stateabb %in% stateIDs$stateabb] 

# there are isolates (# of states in el smaller than IDs); so need to add them
# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1970), directed=F)
str(g.el)

# get isolates
isol <- stateIDs$stateabb[!stateIDs$stateabb %in% uniq.el$stateabb]

# add them
g <- add.vertices(g.el, nv=length(isol), name=isol[1:length(isol)])

# check
str(g)

# get adjacency matrix
gAM <- as.matrix(get.adjacency(g, type="both"))

# make sure vertex order is right
ordAM <- gAM[stateIDs$stateabb,stateIDs$stateabb,drop=FALSE]

# save
write.csv(ordAM, "ally1970.csv")


### For 1975

# get IDs for all states in the dip data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1975,]

# create alliance edgelist
el1975 <- ally1975[,c("stateabb1", "stateabb2")]

# check number of states in el
uniq.el <- data.frame(unique(c(el1975$stateabb1, el1975$stateabb2)))
colnames(uniq.el) <- "stateabb"

# check if all states in el have stateIDs
test <- uniq.el$stateabb %in% stateIDs$stateabb
table(test)
uniq.el$stateabb[!uniq.el$stateabb %in% stateIDs$stateabb] 

# MNC, SNM and LIE are not (ok, not UN members yet); remove them

bad <- el1975[el1975$stateabb1=="MNC"|el1975$stateabb2=="MNC"|el1975$stateabb1=="SNM"|el1975$stateabb2=="SNM"|
                el1975$stateabb1=="LIE"|el1975$stateabb2=="LIE",]

el1975 <- el1975[!el1975$stateabb1=="MNC" & !el1975$stateabb2=="MNC" & !el1975$stateabb1=="SNM" & !el1975$stateabb2=="SNM" &
                   !el1975$stateabb1=="LIE" & !el1975$stateabb2=="LIE",]

# there are isolates (# of states in el smaller than IDs); so need to add them
# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1975), directed=F)
str(g.el)

# get isolates
isol <- stateIDs$stateabb[!stateIDs$stateabb %in% uniq.el$stateabb]

# add them
g <- add.vertices(g.el, nv=length(isol), name=isol[1:length(isol)])

# check
str(g)

# get adjacency matrix
gAM <- as.matrix(get.adjacency(g, type="both"))

# make sure vertex order is right
ordAM <- gAM[stateIDs$stateabb,stateIDs$stateabb,drop=FALSE]

# save
write.csv(ordAM, "ally1975.csv")


### For 1980

# get IDs for all states in the dip data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1980,]

# create alliance edgelist
el1980 <- ally1980[,c("stateabb1", "stateabb2")]

# check number of states in el
uniq.el <- data.frame(unique(c(el1980$stateabb1, el1980$stateabb2)))
colnames(uniq.el) <- "stateabb"

# check if all states in el have stateIDs
test <- uniq.el$stateabb %in% stateIDs$stateabb
table(test)
uniq.el$stateabb[!uniq.el$stateabb %in% stateIDs$stateabb] 

# MNC, SNM and LIE are not (ok, not UN members yet); remove them

bad <- el1980[el1980$stateabb1=="MNC"|el1980$stateabb2=="MNC"|el1980$stateabb1=="SNM"|el1980$stateabb2=="SNM"|
                el1980$stateabb1=="LIE"|el1980$stateabb2=="LIE",]

el1980 <- el1980[!el1980$stateabb1=="MNC" & !el1980$stateabb2=="MNC" & !el1980$stateabb1=="SNM" & !el1980$stateabb2=="SNM" &
                   !el1980$stateabb1=="LIE" & !el1980$stateabb2=="LIE",]

# there are isolates (# of states in el smaller than IDs); so need to add them
# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1980), directed=F)
str(g.el)

# get isolates
isol <- stateIDs$stateabb[!stateIDs$stateabb %in% uniq.el$stateabb]

# add them
g <- add.vertices(g.el, nv=length(isol), name=isol[1:length(isol)])

# check
str(g)

# get adjacency matrix
gAM <- as.matrix(get.adjacency(g, type="both"))

# make sure vertex order is right
ordAM <- gAM[stateIDs$stateabb,stateIDs$stateabb,drop=FALSE]

# save
write.csv(ordAM, "ally1980.csv")


### For 1985

# get IDs for all states in the dip data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1985,]

# create edgelist
el1985 <- ally1985[,c("stateabb1", "stateabb2")]

# check number of states in el
uniq.el <- data.frame(unique(c(el1985$stateabb1, el1985$stateabb2)))
colnames(uniq.el) <- "stateabb"

# check if all states in el have stateIDs
test <- uniq.el$stateabb %in% stateIDs$stateabb
table(test)
uniq.el$stateabb[!uniq.el$stateabb %in% stateIDs$stateabb] 

# MNC, SNM and LIE are not (ok, not UN members yet); remove them

bad <- el1985[el1985$stateabb1=="MNC"|el1985$stateabb2=="MNC"|el1985$stateabb1=="SNM"|el1985$stateabb2=="SNM"|
                el1985$stateabb1=="LIE"|el1985$stateabb2=="LIE",]

el1985 <- el1985[!el1985$stateabb1=="MNC" & !el1985$stateabb2=="MNC" & !el1985$stateabb1=="SNM" & !el1985$stateabb2=="SNM" &
                   !el1985$stateabb1=="LIE" & !el1985$stateabb2=="LIE",]

# there are isolates (# of states in el smaller than IDs); so need to add them
# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1985), directed=F)
str(g.el)

# get isolates
isol <- stateIDs$stateabb[!stateIDs$stateabb %in% uniq.el$stateabb]

# add them
g <- add.vertices(g.el, nv=length(isol), name=isol[1:length(isol)])

# check
str(g)

# get adjacency matrix
gAM <- as.matrix(get.adjacency(g, type="both"))

# make sure vertex order is right
ordAM <- gAM[stateIDs$stateabb,stateIDs$stateabb,drop=FALSE]

# save
write.csv(ordAM, "ally1985.csv")


### For 1990

# get IDs for all states in the dip data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1990,]

# create edgelist
el1990 <- ally1990[,c("stateabb1", "stateabb2")]

# check number of states in el
uniq.el <- data.frame(unique(c(el1990$stateabb1, el1990$stateabb2)))
colnames(uniq.el) <- "stateabb"

# check if all states in el have stateIDs
test <- uniq.el$stateabb %in% stateIDs$stateabb
table(test)
uniq.el$stateabb[!uniq.el$stateabb %in% stateIDs$stateabb] 

# MNC, LIE, SNM are not (ok, not UN members yet); remove them
# GMY and YEM are not in the DIPCON data (joined the UN in October 1990 and 22 May 1990 respectively)
el1990 <- el1990[!el1990$stateabb1=="MNC" & !el1990$stateabb2=="MNC" &
                   !el1990$stateabb1=="SNM" & !el1990$stateabb2=="SNM" &
                   !el1990$stateabb1=="LIE" & !el1990$stateabb2=="LIE" &
                   !el1990$stateabb1=="GMY" & !el1990$stateabb2=="GMY" &
                   !el1990$stateabb1=="YEM" & !el1990$stateabb2=="YEM",]

# there are isolates (# of states in el smaller than IDs); so need to add them
# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1990), directed=F)
str(g.el)

# get isolates
isol <- stateIDs$stateabb[!stateIDs$stateabb %in% uniq.el$stateabb]

# add them
g <- add.vertices(g.el, nv=length(isol), name=isol[1:length(isol)])

# check
str(g)

# get adjacency matrix
gAM <- as.matrix(get.adjacency(g, type="both"))

# make sure vertex order is right
ordAM <- gAM[stateIDs$stateabb,stateIDs$stateabb,drop=FALSE]

# save
write.csv(ordAM, "ally1990.csv")


### For 1995

# get IDs for all states in the dip data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1995,]

# create edgelist
el1995 <- ally1995[,c("stateabb1", "stateabb2")]

# check number of states in el
uniq.el <- data.frame(unique(c(el1995$stateabb1, el1995$stateabb2)))
colnames(uniq.el) <- "stateabb"

# check if all states in el have stateIDs
test <- uniq.el$stateabb %in% stateIDs$stateabb
table(test)
uniq.el$stateabb[!uniq.el$stateabb %in% stateIDs$stateabb] 

# MNC, LIE, SNM, AND are not (microstates?); remove them
el1995 <- el1995[!el1995$stateabb1=="MNC" & !el1995$stateabb2=="MNC" &
                   !el1995$stateabb1=="SNM" & !el1995$stateabb2=="SNM" &
                   !el1995$stateabb1=="LIE" & !el1995$stateabb2=="LIE" &
                   !el1995$stateabb1=="AND" & !el1995$stateabb2=="AND",]

# there are isolates (# of states in el smaller than IDs); so need to add them
# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1995), directed=F)
str(g.el)

# get isolates
isol <- stateIDs$stateabb[!stateIDs$stateabb %in% uniq.el$stateabb]

# add them
g <- add.vertices(g.el, nv=length(isol), name=isol[1:length(isol)])

# check
str(g)

# get adjacency matrix
gAM <- as.matrix(get.adjacency(g, type="both"))

# make sure vertex order is right
ordAM <- gAM[stateIDs$stateabb,stateIDs$stateabb,drop=FALSE]

# save
write.csv(ordAM, "ally1995.csv")


### For 2000

# get IDs for all states in the dip data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==2000,]

# create edgelist
el2000 <- ally2000[,c("stateabb1", "stateabb2")]

# check number of states in el
uniq.el <- data.frame(unique(c(el2000$stateabb1, el2000$stateabb2)))
colnames(uniq.el) <- "stateabb"

# check if all states in el have stateIDs
test <- uniq.el$stateabb %in% stateIDs$stateabb
table(test)
uniq.el$stateabb[!uniq.el$stateabb %in% stateIDs$stateabb] 

# MNC, LIE, SNM, AND are not (microstates?); remove them
el2000 <- el2000[!el2000$stateabb1=="MNC" & !el2000$stateabb2=="MNC" &
                   !el2000$stateabb1=="SNM" & !el2000$stateabb2=="SNM" &
                   !el2000$stateabb1=="LIE" & !el2000$stateabb2=="LIE" &
                   !el2000$stateabb1=="AND" & !el2000$stateabb2=="AND",]

# there are isolates (# of states in el smaller than IDs); so need to add them
# create graph from edge list
g.el <- graph.edgelist(as.matrix(el2000), directed=F)
str(g.el)

# get isolates
isol <- stateIDs$stateabb[!stateIDs$stateabb %in% uniq.el$stateabb]

# add them
g <- add.vertices(g.el, nv=length(isol), name=isol[1:length(isol)])

# check
str(g)

# get adjacency matrix
gAM <- as.matrix(get.adjacency(g, type="both"))

# make sure vertex order is right
ordAM <- gAM[stateIDs$stateabb,stateIDs$stateabb,drop=FALSE]

# save
write.csv(ordAM, "ally2000.csv")


### For 2005

# get IDs for all states in the dip data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==2005,]

# create edgelist
el2003 <- ally2003[,c("stateabb1", "stateabb2")]

# check number of states in el
uniq.el <- data.frame(unique(c(el2003$stateabb1, el2003$stateabb2)))
colnames(uniq.el) <- "stateabb"

# check if all states in el have stateIDs
test <- uniq.el$stateabb %in% stateIDs$stateabb
table(test)
uniq.el$stateabb[!uniq.el$stateabb %in% stateIDs$stateabb] 

# MNC, LIE, SNM, AND are not (microstates?); remove them
el2003 <- el2003[!el2003$stateabb1=="MNC" & !el2003$stateabb2=="MNC" &
                   !el2003$stateabb1=="SNM" & !el2003$stateabb2=="SNM" &
                   !el2003$stateabb1=="LIE" & !el2003$stateabb2=="LIE" &
                   !el2003$stateabb1=="AND" & !el2003$stateabb2=="AND",]

# there are isolates (# of states in el smaller than IDs); so need to add them
# create graph from edge list
g.el <- graph.edgelist(as.matrix(el2003), directed=F)
str(g.el)

# get isolates
isol <- stateIDs$stateabb[!stateIDs$stateabb %in% uniq.el$stateabb]

# add them
g <- add.vertices(g.el, nv=length(isol), name=isol[1:length(isol)])

# check
str(g)

# get adjacency matrix
gAM <- as.matrix(get.adjacency(g, type="both"))

# make sure vertex order is right
ordAM <- gAM[stateIDs$stateabb,stateIDs$stateabb,drop=FALSE]

# save
write.csv(ordAM, "ally2003.csv")


########################################################################
########### Converts COW trade data into dyadic covariate  #############
########################################################################

### Accomplishes 2 tasks:
#### 1) Inserts COW-character country code
#### 2) For every year in  DIPCON data, creates a dyadic covariate (tradeXXXX.csv")

########################################################################

# Read in original data
data <- read.csv("dyadic_trade_3.0.csv",stringsAsFactors=F)

# check column names
names(data)

# create total flow variable
data$trade <- data$flow1+data$flow2
summary(data$trade)

# drop missing values (originally -9)
comp <- data[data$trade>0,]

# insert log_trade variable
comp$log_trade <- log(comp$trade)


#### 1) Insert COW-character country code 

# Convert to COW-character
comp$stateabb1 <- countrycode(comp$ccode1, "cown", "cowc")
comp$stateabb2 <- countrycode(comp$ccode2, "cown", "cowc")

# Check if all states have this code
table(is.na(comp$stateabb1))
comp[is.na(comp$stateabb1),] # ok, only Hong Kong and Taiwan
table(is.na(comp$stateabb2))


#### 2) For every year in DIPCON data, create a dyadic covariate ("contXXXX.csv")

# select DIPCON data years
table(comp$year)

trade1970 <- comp[comp$year=="1970",]
trade1975 <- comp[comp$year=="1975",]
trade1980 <- comp[comp$year=="1980",]
trade1985 <- comp[comp$year=="1985",]
trade1990 <- comp[comp$year=="1990",]
trade1995 <- comp[comp$year=="1995",]
trade2000 <- comp[comp$year=="2000",]
trade2005 <- comp[comp$year=="2005",]

summary(trade1985$log_trade)

myvars <- c("stateabb1","stateabb2","log_trade")


### For 1970

# create edgelist
trade1970 <- trade1970[names(trade1970) %in% myvars]
el1970 <- trade1970[,c("stateabb1", "stateabb2")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1970,]
stateIDs <- stateIDs$stateabb

# check if all states are in the edgelist 
uniq.el <- unique(c(el1970$stateabb1, el1970$stateabb2)) # 133 states
uniq.el[!uniq.el %in% stateIDs] 

# ZIM and FIJ are not stateIDs (not UN members); remove them from edgelist and trade data
el1970 <- el1970[!el1970$stateabb1=="ZIM" & !el1970$stateabb2=="ZIM" &
                   !el1970$stateabb1=="FIJ" & !el1970$stateabb2=="FIJ",]

trade1970 <- trade1970[!trade1970$stateabb1=="ZIM" & !trade1970$stateabb2=="ZIM" &
                         !trade1970$stateabb1=="FIJ" & !trade1970$stateabb2=="FIJ",]

stateIDs[!stateIDs %in% uniq.el] # BHU, DRV, WSM are isolates (not in the trade network)
# there are NAs (# of states in el smaller than IDs); so need to create graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1970), directed=F)
str(g.el)

# unite graphs to create one with all states, including NAs
g <- graph.union(g, g.el)

# check
str(g)

# set edge weights
E(g)$weight <- trade1970$log_trade

# get adjacency matrix
gAM <- get.adjacency(g, type="both", attr="weight")

write.csv(as.matrix(gAM), "trade1970.csv")


### For 1975

# create edgelist
trade1975 <- trade1975[names(trade1975) %in% myvars]
el1975 <- trade1975[,c("stateabb1", "stateabb2")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1975,]
stateIDs <- stateIDs$stateabb

# check if all states are in the edgelist 
uniq.el <- unique(c(el1975$stateabb1, el1975$stateabb2)) # 148 states
uniq.el[!uniq.el %in% stateIDs] 

# SUR, CAP and STP are not stateIDs (not UN members); remove them from edgelist and trade data
el1975 <- el1975[!el1975$stateabb1=="SUR" & !el1975$stateabb2=="SUR" &
                   !el1975$stateabb1=="CAP" & !el1975$stateabb2=="CAP" &
                   !el1975$stateabb1=="STP" & !el1975$stateabb2=="STP",]

trade1975 <- trade1975[!trade1975$stateabb1=="SUR" & !trade1975$stateabb2=="SUR" &
                         !trade1975$stateabb1=="CAP" & !trade1975$stateabb2=="CAP" &
                         !trade1975$stateabb1=="STP" & !trade1975$stateabb2=="STP",]

stateIDs[!stateIDs %in% uniq.el] # BHU, DRV, WSM are isolates (not in the trade network)
# there are NAs (# of states in el smaller than IDs); so need to create graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1975), directed=F)
str(g.el)

# unite graphs to create one with all states, including NAs
g <- graph.union(g, g.el)

# check
str(g)

# set edge weights
E(g)$weight <- trade1975$log_trade

# get adjacency matrix
gAM <- get.adjacency(g, type="both", attr="weight")

write.csv(as.matrix(gAM), "trade1975.csv")


### For 1980

# create edgelist
trade1980 <- trade1980[names(trade1980) %in% myvars]
el1980 <- trade1980[,c("stateabb1", "stateabb2")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1980,]
stateIDs <- stateIDs$stateabb

# check if all states are in the edgelist 
uniq.el <- unique(c(el1980$stateabb1, el1980$stateabb2)) # 155 states
uniq.el[!uniq.el %in% stateIDs] # no NAs

stateIDs[!stateIDs %in% uniq.el] # BHU and BRU are isolates (not in the trade network)
# there are NAs (# of states in el smaller than IDs); so need to create graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1980), directed=F)
str(g.el)

# unite graphs to create one with all states, including NAs
g <- graph.union(g, g.el)

# check
str(g)

# set edge weights
E(g)$weight <- trade1980$log_trade

# get adjacency matrix
gAM <- get.adjacency(g, type="both", attr="weight")

write.csv(as.matrix(gAM), "trade1980.csv")


### For 1985

# create edgelist
trade1985 <- trade1985[names(trade1985) %in% myvars]
el1985 <- trade1985[,c("stateabb1", "stateabb2")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1985,]
stateIDs <- stateIDs$stateabb

# check if all states are in the edgelist 
uniq.el <- unique(c(el1985$stateabb1, el1985$stateabb2)) # 155 states
uniq.el[!uniq.el %in% stateIDs] # no NAs

stateIDs[!stateIDs %in% uniq.el] # BHU and NAM are isolates (not in the trade network)
# there are NAs (# of states in el smaller than IDs); so need to create graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1985), directed=F)
str(g.el)

# unite graphs to create one with all states, including NAs
g <- graph.union(g, g.el)

# check
str(g)

# set edge weights
E(g)$weight <- trade1985$log_trade

# get adjacency matrix
gAM <- get.adjacency(g, type="both", attr="weight")

write.csv(as.matrix(gAM), "trade1985.csv")


### For 1990

# create edgelist
trade1990 <- trade1990[names(trade1990) %in% myvars]
el1990 <- trade1990[,c("stateabb1", "stateabb2")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1990,]
stateIDs <- stateIDs$stateabb

# check if all states are in the edgelist 
uniq.el <- unique(c(el1990$stateabb1, el1990$stateabb2)) # 161 states
uniq.el[!uniq.el %in% stateIDs] 

# LIE and YEM are not stateIDs (not UN members); remove them from edgelist and trade data
el1990 <- el1990[!el1990$stateabb1=="LIE" & !el1990$stateabb2=="LIE" &
                   !el1990$stateabb1=="YEM" & !el1990$stateabb2=="YEM",]

trade1990 <- trade1990[!trade1990$stateabb1=="LIE" & !trade1990$stateabb2=="LIE" &
                         !trade1990$stateabb1=="YEM" & !trade1990$stateabb2=="YEM",]

stateIDs[!stateIDs %in% uniq.el] # "GDR" "YAR" "YPR" "MSI" "FSM" are isolates (not in the trade network)
# there are NAs (# of states in el smaller than IDs); so need to create graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1990), directed=F)
str(g.el)

# unite graphs to create one with all states, including NAs
g <- graph.union(g, g.el)

# check
str(g)

# set edge weights
E(g)$weight <- trade1990$log_trade

# get adjacency matrix
gAM <- get.adjacency(g, type="both", attr="weight")

write.csv(as.matrix(gAM), "trade1990.csv")


### For 1995

# create edgelist
trade1995 <- trade1995[names(trade1995) %in% myvars]
el1995 <- trade1995[,c("stateabb1", "stateabb2")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1995,]
stateIDs <- stateIDs$stateabb

# check if all states are in the edgelist 
uniq.el <- unique(c(el1995$stateabb1, el1995$stateabb2)) # 186 states
uniq.el[!uniq.el %in% stateIDs] 

# "MNC" "LIE" "AND" "SNM" are not stateIDs; remove them from edgelist and trade data
el1995 <- el1995[!el1995$stateabb1=="MNC" & !el1995$stateabb2=="MNC" &
                   !el1995$stateabb1=="LIE" & !el1995$stateabb2=="LIE" &
                   !el1995$stateabb1=="AND" & !el1995$stateabb2=="AND" &
                   !el1995$stateabb1=="SNM" & !el1995$stateabb2=="SNM",]

trade1995 <- trade1995[!trade1995$stateabb1=="MNC" & !trade1995$stateabb2=="MNC" &
                         !trade1995$stateabb1=="LIE" & !trade1995$stateabb2=="LIE" &
                         !trade1995$stateabb1=="AND" & !trade1995$stateabb2=="AND" &
                         !trade1995$stateabb1=="SNM" & !trade1995$stateabb2=="SNM",]

stateIDs[!stateIDs %in% uniq.el] # FSM are isolates (not in the trade network)
# there are NAs (# of states in el smaller than IDs); so need to create graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1995), directed=F)
str(g.el)

# unite graphs to create one with all states, including NAs
g <- graph.union(g, g.el)

# check
str(g)

# set edge weights
E(g)$weight <- trade1995$log_trade

# get adjacency matrix
gAM <- get.adjacency(g, type="both", attr="weight")

write.csv(as.matrix(gAM), "trade1995.csv")


### For 2000

# create edgelist
trade2000 <- trade2000[names(trade2000) %in% myvars]
el2000 <- trade2000[,c("stateabb1", "stateabb2")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==2000,]
stateIDs <- stateIDs$stateabb

# check if all states are in the edgelist 
uniq.el <- unique(c(el2000$stateabb1, el2000$stateabb2)) # 190 states
uniq.el[!uniq.el %in% stateIDs] 

# "MNC" "LIE" "AND" "SNM" are not stateIDs; remove them from edgelist and trade data
el2000 <- el2000[!el2000$stateabb1=="MNC" & !el2000$stateabb2=="MNC" &
                   !el2000$stateabb1=="LIE" & !el2000$stateabb2=="LIE" &
                   !el2000$stateabb1=="AND" & !el2000$stateabb2=="AND" &
                   !el2000$stateabb1=="SNM" & !el2000$stateabb2=="SNM",]

trade2000 <- trade2000[!trade2000$stateabb1=="MNC" & !trade2000$stateabb2=="MNC" &
                         !trade2000$stateabb1=="LIE" & !trade2000$stateabb2=="LIE" &
                         !trade2000$stateabb1=="AND" & !trade2000$stateabb2=="AND" &
                         !trade2000$stateabb1=="SNM" & !trade2000$stateabb2=="SNM",]

stateIDs[!stateIDs %in% uniq.el] # FSM are isolates (not in the trade network)
# there are NAs (# of states in el smaller than IDs); so need to create graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el2000), directed=F)
str(g.el)

# unite graphs to create one with all states, including NAs
g <- graph.union(g, g.el)

# check
str(g)

# set edge weights
E(g)$weight <- trade2000$log_trade

# get adjacency matrix
gAM <- get.adjacency(g, type="both", attr="weight")

write.csv(as.matrix(gAM), "trade2000.csv")


### For 2005

# create edgelist
trade2005 <- trade2005[names(trade2005) %in% myvars]
el2005 <- trade2005[,c("stateabb1", "stateabb2")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==2005,]
stateIDs <- stateIDs$stateabb

# check if all states are in the edgelist 
uniq.el <- unique(c(el2005$stateabb1, el2005$stateabb2)) # 192 states
uniq.el[!uniq.el %in% stateIDs] 

# "MNC" "LIE" "AND" "SNM" are not stateIDs; remove them from edgelist and trade data
el2005 <- el2005[!el2005$stateabb1=="MNC" & !el2005$stateabb2=="MNC" &
                   !el2005$stateabb1=="LIE" & !el2005$stateabb2=="LIE" &
                   !el2005$stateabb1=="AND" & !el2005$stateabb2=="AND" &
                   !el2005$stateabb1=="SNM" & !el2005$stateabb2=="SNM",]

trade2005 <- trade2005[!trade2005$stateabb1=="MNC" & !trade2005$stateabb2=="MNC" &
                         !trade2005$stateabb1=="LIE" & !trade2005$stateabb2=="LIE" &
                         !trade2005$stateabb1=="AND" & !trade2005$stateabb2=="AND" &
                         !trade2005$stateabb1=="SNM" & !trade2005$stateabb2=="SNM",]

stateIDs[!stateIDs %in% uniq.el] # no isolates 
g <- graph.edgelist(as.matrix(el2005), directed=F)
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# set edge weights
E(g)$weight <- trade2005$log_trade

# get adjacency matrix
gAM <- get.adjacency(g, type="both", attr="weight")

write.csv(as.matrix(gAM), "trade2005.csv")


########################################################################
########### Converts contiguity data into dyadic covariate  ############
########################################################################

### Accomplishes 3 tasks:
#### 1) Recodes contiguity to take maritime borders into account
#### 2) Re-inserts COW-character country code (to fix errors)
#### 3) For every year in DIPCON data, creates a dyadic covariate ("contXXXX.csv")

########################################################################

# Read in original data
cont <- read.csv("contdird.csv",stringsAsFactors=F)

# check column names
names(cont)


#### 1) Recode contiguity to take maritime borders into account

# Does COW code maritime distance in miles instead of nautical miles (as done in customary int'l law and the UN
# Convention on the Law of the Sea)? In the Convention, territorial waters are 12 nmi (14 mi) from the baseline, and
# contiguous zones (where states can enforce laws in four specific areas) are 24 nmi (28 mi) from the baseline.
# I'd like to code as contiguous states that are 24 nmi away from each other. Is that what the COW data code?

# recode contiguity type
table(cont$conttype)
cont$type <- cont$conttype

# 0 if separated by more than 24 nautical miles
cont$type[cont$conttype > 3] <- 0

# 1 if sharing land, river, or maritime border (up to 24 nmi)
cont$type[cont$conttype < 4] <- 1

# check
table(cont$type)


#### 2) Re-insert COW-character country code (to fix errors)

# Convert to COW-character
cont$stateabb1 <- countrycode(cont$state1no, "cown", "cowc")
cont$stateabb2 <- countrycode(cont$state2no, "cown", "cowc")

# Check if all states have this code
table(is.na(cont$stateabb1))
cont[is.na(cont$stateabb1),]
table(is.na(cont$stateabb2))

# code for Montenegro wrong
cont$stateabb1[cont$state1ab=="MTG"] <- "MNG"
cont$stateabb2[cont$state2ab=="MTG"] <- "MNG"


#### 3) For every year in DIPCON data, create a dyadic covariate ("contXXXX.csv")

# select DIPCON data years
table(cont$year)

dcont1970 <- cont[cont$year=="1970",]
dcont1975 <- cont[cont$year=="1975",]
dcont1980 <- cont[cont$year=="1980",]
dcont1985 <- cont[cont$year=="1985",]
dcont1990 <- cont[cont$year=="1990",]
dcont1995 <- cont[cont$year=="1995",]
dcont2000 <- cont[cont$year=="2000",]
dcont2005 <- cont[cont$year=="2005",]


### For 1970
length(unique(dcont1970$dyad))

# dyads are duplicated; get unique ones
cont1970 <- dcont1970[!duplicated(dcont1970$dyad), c("stateabb1", "stateabb2", "type")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1970,]
stateIDs <- stateIDs$stateabb

# create edgelist
el1970 <- cont1970[cont1970$type=="1", c("stateabb1", "stateabb2")]

# check if all states are in the edgelist 
uniq.el <- unique(c(el1970$stateabb1, el1970$stateabb2)) # 122 states
uniq.el[!uniq.el %in% stateIDs] # ZIM 

# ZIM is not stateIDs (not UN members); remove them from edgelist 
el1970 <- el1970[!el1970$stateabb1=="ZIM" & !el1970$stateabb2=="ZIM",]

stateIDs[!stateIDs %in% uniq.el] # "CUB" "JAM" "BAR" "MLT" "CYP" "ICE" "MAG" "MAS" "BHU" "MAD" "AUL" "NEW" "WSM"
# are isolates 
# there are isolates (# of states in el smaller than IDs); so need to create another graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1970), directed=F)
str(g.el)

# unite graphs to create one with all states, including isolates
g <- graph.union(g, g.el)

# check
str(g)

# get adjacency matrix
gAM <- get.adjacency(g, type="both")

write.csv(as.matrix(gAM), "cont1970.csv")


### For 1975
length(unique(dcont1975$dyad))

# dyads are duplicated; get unique ones
cont1975 <- dcont1975[!duplicated(dcont1975$dyad), c("stateabb1", "stateabb2", "type")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1975,]
stateIDs <- stateIDs$stateabb

# create edgelist
el1975 <- cont1975[cont1975$type=="1", c("stateabb1", "stateabb2")]

# check if all states are in the edgelist 
uniq.el <- unique(c(el1975$stateabb1, el1975$stateabb2)) # 135 states
uniq.el[!uniq.el %in% stateIDs] # SUR 

# SUR is not stateIDs (not UN members); remove them from edgelist 
el1975 <- el1975[!el1975$stateabb1=="SUR" & !el1975$stateabb2=="SUR",]

stateIDs[!stateIDs %in% uniq.el] #"BHM" "CUB" "JAM" "BAR" "GRN" "MLT" "ICE" "MAG" "COM" "MAS" "MAD" "NEW" "FIJ" "WSM"
# are isolates 
# there are isolates (# of states in el smaller than IDs); so need to create another graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1975), directed=F)
str(g.el)

# unite graphs to create one with all states, including isolates
g <- graph.union(g, g.el)

# check
str(g)

# get adjacency matrix
gAM <- get.adjacency(g, type="both")

write.csv(as.matrix(gAM), "cont1975.csv")


### For 1980
length(unique(dcont1980$dyad))

# dyads are duplicated; get unique ones
cont1980 <- dcont1980[!duplicated(dcont1980$dyad), c("stateabb1", "stateabb2", "type")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1980,]
stateIDs <- stateIDs$stateabb

# create edgelist
el1980 <- cont1980[cont1980$type=="1", c("stateabb1", "stateabb2")]

# check if all states are in the edgelist 
uniq.el <- unique(c(el1980$stateabb1, el1980$stateabb2)) # 139 states
uniq.el[!uniq.el %in% stateIDs] # ok none 

stateIDs[!stateIDs %in% uniq.el] # "BHM" "CUB" "JAM" "BAR" "DMA" "MLT" "ICE" "CAP" "STP" "MAG" "COM" 
# "MAS" "SEY" "MAD" "BRU" "NEW" "FIJ" "WSM" are isolates 
# there are isolates (# of states in el smaller than IDs); so need to create another graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1980), directed=F)
str(g.el)

# unite graphs to create one with all states, including isolates
g <- graph.union(g, g.el)

# check
str(g)

# get adjacency matrix
gAM <- get.adjacency(g, type="both")

write.csv(as.matrix(gAM), "cont1980.csv")


### For 1985
length(unique(dcont1985$dyad))

# dyads are duplicated; get unique ones
cont1985 <- dcont1985[!duplicated(dcont1985$dyad), c("stateabb1", "stateabb2", "type")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1985,]
stateIDs <- stateIDs$stateabb

# create edgelist
el1985 <- cont1985[cont1985$type=="1", c("stateabb1", "stateabb2")]

# check if all states are in the edgelist 
uniq.el <- unique(c(el1985$stateabb1, el1985$stateabb2)) # 143 states
uniq.el[!uniq.el %in% stateIDs] # ok none 

stateIDs[!stateIDs %in% uniq.el] # "BHM" "CUB" "JAM" "BAR" "DMA" "MLT" "ICE" "CAP" "STP" "NAM" "MAG" "COM" 
# "MAS" "SEY" "MAD" "NEW" "VAN" "FIJ" "WSM" are isolates 
# there are isolates (# of states in el smaller than IDs); so need to create another graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1985), directed=F)
str(g.el)

# unite graphs to create one with all states, including isolates
g <- graph.union(g, g.el)

# check
str(g)

# get adjacency matrix
gAM <- get.adjacency(g, type="both")

write.csv(as.matrix(gAM), "cont1985.csv")


### For 1990
length(unique(dcont1990$dyad))

# dyads are duplicated; get unique ones
cont1990 <- dcont1990[!duplicated(dcont1990$dyad), c("stateabb1", "stateabb2", "type")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1990,]
stateIDs <- stateIDs$stateabb

# create edgelist
el1990 <- cont1990[cont1990$type=="1", c("stateabb1", "stateabb2")]

# check if all states are in the edgelist 
uniq.el <- unique(c(el1990$stateabb1, el1990$stateabb2)) # 147 states
uniq.el[!uniq.el %in% stateIDs] # "LIE" "GMY" "YEM" 

# "LIE" "GMY" "YEM" are not stateIDs (not UN members); remove them from edgelist 
el1990 <- el1990[!el1990$stateabb1=="LIE" & !el1990$stateabb2=="LIE" &
                   !el1990$stateabb1=="GMY" & !el1990$stateabb2=="GMY" &
                   !el1990$stateabb1=="YEM" & !el1990$stateabb2=="YEM",]

stateIDs[!stateIDs %in% uniq.el] # "BHM" "CUB" "JAM" "BAR" "DMA" "MLT" "ICE" "CAP" "STP" "MAG" "COM" "MAS" "SEY" 
# "MAD" "NEW" "VAN" "FIJ" "MSI" "FSM" "WSM" are isolates 
# there are isolates (# of states in el smaller than IDs); so need to create another graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1990), directed=F)
str(g.el)

# unite graphs to create one with all states, including isolates
g <- graph.union(g, g.el)

# check
str(g)

# get adjacency matrix
gAM <- get.adjacency(g, type="both")

write.csv(as.matrix(gAM), "cont1990.csv")


### For 1995
length(unique(dcont1995$dyad))

# dyads are duplicated; get unique ones
cont1995 <- dcont1995[!duplicated(dcont1995$dyad), c("stateabb1", "stateabb2", "type")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==1995,]
stateIDs <- stateIDs$stateabb

# create edgelist
el1995 <- cont1995[cont1995$type=="1", c("stateabb1", "stateabb2")]

# check if all states are in the edgelist 
uniq.el <- unique(c(el1995$stateabb1, el1995$stateabb2)) # 166 states
uniq.el[!uniq.el %in% stateIDs] # "LIE" "MNC" "AND" "SNM" 

# MNC, LIE, SNM, AND are not (microstates?); remove them
el1995 <- el1995[!el1995$stateabb1=="MNC" & !el1995$stateabb2=="MNC" &
                   !el1995$stateabb1=="SNM" & !el1995$stateabb2=="SNM" &
                   !el1995$stateabb1=="LIE" & !el1995$stateabb2=="LIE" &
                   !el1995$stateabb1=="AND" & !el1995$stateabb2=="AND",]

stateIDs[!stateIDs %in% uniq.el] # "BHM" "CUB" "JAM" "BAR" "DMA" "MLT" "ICE" "CAP" "STP" "MAG" "COM" 
# "MAS" "SEY" "MAD" "NEW" "VAN" "FIJ" "MSI" "PAL" "FSM" "WSM" are isolates 
# there are isolates (# of states in el smaller than IDs); so need to create another graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el1995), directed=F)
str(g.el)

# unite graphs to create one with all states, including isolates
g <- graph.union(g, g.el)

# check
str(g)

# get adjacency matrix
gAM <- get.adjacency(g, type="both")

write.csv(as.matrix(gAM), "cont1995.csv")


### For 2000
length(unique(dcont2000$dyad))

# dyads are duplicated; get unique ones
cont2000 <- dcont2000[!duplicated(dcont2000$dyad), c("stateabb1", "stateabb2", "type")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==2000,]
stateIDs <- stateIDs$stateabb

# create edgelist
el2000 <- cont2000[cont2000$type=="1", c("stateabb1", "stateabb2")]

# check if all states are in the edgelist 
uniq.el <- unique(c(el2000$stateabb1, el2000$stateabb2)) # 166 states
uniq.el[!uniq.el %in% stateIDs] 

# MNC, LIE, SNM, AND are not (microstates?); remove them
el2000 <- el2000[!el2000$stateabb1=="MNC" & !el2000$stateabb2=="MNC" &
                   !el2000$stateabb1=="SNM" & !el2000$stateabb2=="SNM" &
                   !el2000$stateabb1=="LIE" & !el2000$stateabb2=="LIE" &
                   !el2000$stateabb1=="AND" & !el2000$stateabb2=="AND",]

stateIDs[!stateIDs %in% uniq.el] # "BHM" "CUB" "JAM" "BAR" "DMA" "MLT" "ICE" "CAP" "STP" "MAG" "COM" "MAS" "SEY" 
# "MAD" "NEW" "VAN" "KIR" "TUV" "FIJ" "TON" "NAU" "MSI" "PAL" "FSM" "WSM" are isolates 
# there are isolates (# of states in el smaller than IDs); so need to create another graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el2000), directed=F)
str(g.el)

# unite graphs to create one with all states, including isolates
g <- graph.union(g, g.el)

# check
str(g)

# get adjacency matrix
gAM <- get.adjacency(g, type="both")

write.csv(as.matrix(gAM), "cont2000.csv")


### For 2005
length(unique(dcont2005$dyad))

# dyads are duplicated; get unique ones
cont2005 <- dcont2005[!duplicated(dcont2005$dyad), c("stateabb1", "stateabb2", "type")]

# get IDs for all states in the data
stateIDs <- read.csv("stateIDs.csv", stringsAsFactors=F)
stateIDs <- stateIDs[stateIDs$year==2005,]
stateIDs <- stateIDs$stateabb

# create edgelist
el2005 <- cont2005[cont2005$type=="1", c("stateabb1", "stateabb2")]

# check if all states are in the edgelist 
uniq.el <- unique(c(el2005$stateabb1, el2005$stateabb2)) # 167 states
uniq.el[!uniq.el %in% stateIDs] # "LIE" "MNC" "AND" "SNM" 

# MNC, LIE, SNM, AND are not (microstates?); remove them
el2005 <- el2005[!el2005$stateabb1=="MNC" & !el2005$stateabb2=="MNC" &
                   !el2005$stateabb1=="SNM" & !el2005$stateabb2=="SNM" &
                   !el2005$stateabb1=="LIE" & !el2005$stateabb2=="LIE" &
                   !el2005$stateabb1=="AND" & !el2005$stateabb2=="AND",]

stateIDs[!stateIDs %in% uniq.el] # "BHM" "CUB" "JAM" "BAR" "DMA" "MLT" "ICE" "CAP" "STP" "NAM" "MAG" "COM" 
# "MAS" "SEY" "MAD" "NEW" "VAN" "FIJ" "WSM" are isolates 
# there are isolates (# of states in el smaller than IDs); so need to create another graph w/ all states
g <- graph.empty(n=0, directed=F)
g <- add.vertices(g, nv=length(stateIDs))
g <- set.vertex.attribute(g, name="name", index=V(g), stateIDs)

# create graph from edge list
g.el <- graph.edgelist(as.matrix(el2005), directed=F)
str(g.el)

# unite graphs to create one with all states, including isolates
g <- graph.union(g, g.el)

# check
str(g)

# get adjacency matrix
gAM <- get.adjacency(g, type="both")

write.csv(as.matrix(gAM), "cont2005.csv")


